import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import Login from "@/views/login.vue";
import Main from "@/views/Main.vue";
import Register from "../views/register.vue";

// import {sysStore,useMenuInfoStore} from "../stores";

// 静态路由
const staticRoutes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: Login
    },
    {
        path:'/login',
        name:'login',
        component: Login
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
    },
    {
        path: '/main',
        name: 'main',
        component: Main,
        redirect: '/index',
        children: [
            { path: "/index", name: "工作台", component: () => import("../views/home/index.vue") },
            { path: "/profile", name: "个人中心", component: () => import("../views/profile/index.vue"), },
            { path: "/toCategory", name: "分类管理", component: () => import("../views/goods/category/index.vue"), },
            { path: "/toSwiper", name: "轮播图配置", component: () => import("../views/config/index.vue"), },
            { path: "/toMerchant", name: "商铺管理", component: () => import("../views/shop/merchant/index.vue"), },
            { path: "/toMyMerchant", name: "我的店铺", component: () => import("../views/shop/myMerchant/index.vue"), },
            { path: "/toReviews", name: "评价管理", component: () => import("../views/goods/reviews/index.vue"), },
            { path: "/toInventory", name: "库存管理", component: () => import("../views/shop/inventory/index.vue"), },
            { path: "/toProduct", name: "商品列表", component: () => import("../views/goods/product/index.vue"), },
            { path: "/toOrder", name: "订单列表", component: () => import("../views/order/order/index.vue"), },
            { path: "/toArticles", name: "文章列表", component: () => import("../views/community/article/index.vue"), },
            { path: "/toUsers", name: "用户管理", component: () => import("../views/user/users/index.vue"), },
            { path: "/toRoles", name: "角色管理", component: () => import("../views/user/roles/index.vue"), },
            { path: "/toPermissions", name: "权限管理", component: () => import("../views/user/permission/index.vue"), },
            { path: "/toFeedback", name: "反馈管理", component: () => import("../views/feedback/feedback/index.vue"), }
        ]
    },

];

const router = createRouter({
    history: createWebHistory(),
    routes: staticRoutes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
    // const menuStore= useMenuInfoStore();
    // const routerStore = sysStore();
    from //暂未使用
    if (to.path === '/login' || to.path === '/register') {
        next(); // 放行登录和注册页面
    } else if (!isAuthenticated()) {
        next('/login'); // 重定向到登录页面
    } else if (to.path === '/' && isAuthenticated()){
        next('/main')
    } else {
        next(); // 放行其他已登录用户可以访问的页面
    }
        // const menuFlag= menuStore.getMenuFlag;
        //判断是否为首页并且没有获取过菜单权限
        // if(!menuFlag ) {
        //     // const modules = import.meta.glob("../views/**/*.vue");
        //     //获取菜单
        //     const menuList= await menuStore.getMenu
        //     routerStore.setRouterList(menuList)
        //     router.addRoute({
        //         path: '/main',
        //         name: 'main',
        //         component: Main,
        //         redirect: '/main/index',
        //         children: routerStore.routerList,
        //     },);
        //
        //     console.log("添加完后的路由",router.getRoutes());
        //     next({...to,replace:true})
        //     return
        // }
});

// function addRoute(menuInfo: any, modules: any)  {
//     const menu = {
//         path: menuInfo.path,
//         name: menuInfo.title,
//         component: modules[`/src/views${menuInfo.path}.vue`], // 动态导入组件
//     };
//     // 添加路由
//     RouterList.push(menu);
// }

// 打印所有路由信息
// console.log('所有路由？:', router.getRoutes().map(route => ({
//     path: route.path,
//     name: route.name,
//     component: route.components,
//     children: route.children ? route.children.map(child => ({
//         path: child.path,
//         name: child.name,
//         component: child.component
//     })) : null
// })));

// 判断是否登录的函数
function isAuthenticated() {
    const token = localStorage.getItem('AccessToken')
    return !(token === '' || token === null);
}

// 将router暴露出去
export default router